package com.pan.server.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.pan.common.constant.MessageConstant;
import com.pan.common.exception.OrderNotFoundException;
import com.pan.common.exception.StockIsNotEnoughException;
import com.pan.pojo.vo.OrderVO;
import com.pan.server.mapper.BookMapper;
import com.pan.server.mapper.OrdersMapper;
import com.pan.server.mapper.UserMapper;
import com.pan.server.service.OrdersService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.http.HttpMessageConverters;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
@Slf4j
public class OrdersServiceImpl implements OrdersService {

    @Autowired
    private OrdersMapper ordersMapper;
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private BookMapper booksMapper;
    @Autowired
    private HttpMessageConverters messageConverters;


    /**
     * 查询订单列表
     * @param username
     * @param pageNum
     * @param pageSize
     * @return
     */
    @Override
    public PageInfo<OrderVO> queryOrders(String username, Integer pageNum, Integer pageSize) {

        PageHelper.startPage(pageNum, pageSize);

        Long userId = userMapper.selectUserIDByUsername(username);

        List<OrderVO> list = ordersMapper.queryOrders(userId);

        PageInfo<OrderVO> pageInfo = new PageInfo<>(list);

        return pageInfo;
    }


    /**
     * 添加订单
     * @param username
     * @param bookId
     * @param quantity
     */
    @Override
    public void addOrder(String username, Integer bookId, Integer quantity) {

        Long userId = userMapper.selectUserIDByUsername(username);
        double price = booksMapper.searchPriceById(bookId);
        LocalDateTime now = LocalDateTime.now();
        // 设置时间格式
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");

        // 格式化日期
        String formattedDate = now.format(formatter);


        Map<String, Object> map = new HashMap<>();
        map.put("userId", userId);
        map.put("now", formattedDate);

        ordersMapper.addOrder(map);
        Long id = ordersMapper.searchOrders(map);
        Map<String, Object> map2 = new HashMap<>();
        map2.put("orderId", id);
        map2.put("bookId", bookId);
        map2.put("quantity", quantity);
        map2.put("price", price);
        ordersMapper.addOrderDetail(map2);
    }

    /**
     * 更新订单状态
     * @param orderId
     * @param status
     */
    @Override
    public void updateOrder(Integer orderId, String status) {

        if(status.equals("已完成")){
            //查询订单的书籍数量
            int stock=booksMapper.queryBookStock(orderId);

            int bookId=booksMapper.queryBookIdByOrderId(orderId);

            if(stock<ordersMapper.queryOrderQuantity(orderId)){
                throw new StockIsNotEnoughException(MessageConstant.STOCK_IS_NOT_ENOUGH);
            }

            ordersMapper.updateBookStock(orderId);
        }

        Map<String, Object> map = new HashMap<>();
        map.put("orderId", orderId);
        map.put("status", status);
        ordersMapper.updateOrder(map);



    }

    /**
     * 删除订单
     * @param orderId
     */
    @Override
    public void deleteOrder(Integer orderId) {

        OrderVO orderVO=ordersMapper.queryOrderById(orderId);

        if(orderVO==null){
            throw new OrderNotFoundException(MessageConstant.ORDER_NOT_FOUND);
        }

        ordersMapper.deleteOrderDetail(orderId);

        ordersMapper.deleteOrder(orderId);

    }
}